// ignore_for_file: non_constant_identifier_names
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../common_ui/at_switch.dart';
import '../../common_ui/back_nav.dart';
import '../../common_ui/loading_view.dart';
import '../../request/api.dart';
import '../../request/requestManager.dart';
import '../../style/edgeInsets_style.dart';
import '../../style/text_style.dart';

class PrivacySetPage extends StatefulWidget {
  const PrivacySetPage({super.key});

  @override
  State<PrivacySetPage> createState() => _PrivacySetPageState();
}

class _PrivacySetPageState extends State<PrivacySetPage> {
  List<Map> privacyList = [
    {'title': '隐藏榜单', 'value': 1}, //是否隐藏排行 1：隐藏 2：不隐藏
  ];
  bool loading = true;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BackNavigation(
        title: '隐私设置',
      ),
      body: loading == false
          ? ListView.builder(
              itemBuilder: (context, index) {
                return setItemWidget(index);
              },
              itemCount: privacyList.length,
            )
          : const LoadingView(),
    );
  }

  Widget setItemWidget(int index) {
    Map item = privacyList[index];
    return Container(
      padding: EdgeInsetsStyle.edgeInsets(20, 20),
      height: 70.w,
      alignment: Alignment.center,
      child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
        Text(
          item['title'],
          style: Styles.normalText_15,
        ),
        CommonSwitchWidget(
            isSelect: setSwitchValue(item),
            switchCallBack: (e) {
              debugPrint('e:$e');
              editSwitchValue(index, e);
            })
      ]),
    );
  }

  void editSwitchValue(int index, bool value) async {
    Map parameters;
    if (index == 0) {
      parameters = {'hide_rank_status': value == true ? 1 : 2};
    } else if (index == 1) {
      parameters = {'hide_gift_status': value == true ? 1 : 2};
    } else if (index == 2) {
      parameters = {'hide_guard_status': value == true ? 1 : 2};
    } else {
      parameters = {'order_switch': value == true ? 1 : 2};
    }
    var result =
        await RequestManager().post(API.setHideStatus, params: parameters);
    if (result != null && result['code'] == 0) {
      privacyList[index]['value'] = value == true ? 1 : 2;
      setState(() {});
    }
    if (result != null && result['code'] == 1) {
      privacyList[index]['value'] = !value;
      setState(() {});
    }
  }

  bool setSwitchValue(Map item) {
    return item['value'] == 1;
  }

  @override
  void initState() {
    super.initState();
    getPrivacySet();
  }

  void getPrivacySet() async {
    var result = await RequestManager().post(API.myInfo);
    if (result != null && result['code'] == 0) {
      loading = false;
      Map data = result['data'];
      Map anchor = data['anchor'];
      privacyList[0]['value'] = anchor['hide_rank_status'];

      Map userInfo = data['userinfo'];
      int gender = userInfo['gender'];
      if (gender == 1) {
        privacyList.add(
          {'title': '隐藏收到的礼物', 'value': anchor['hide_gift_status']},
        );
        privacyList.add(
          {'title': '隐藏收到的守护', 'value': anchor['hide_guard_status']},
        );
        privacyList.add(
          {'title': '隐藏通话次数', 'value': anchor['order_switch']},
        );
      }
      setState(() {});
    }
  }
}
